package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("online")
public class OnLineController {

    //得到在线人数
    @RequestMapping("getNum")
    public String getNum(HttpServletRequest request){
        HttpSession session = request.getSession();
        //从上下文中得到在线人数
        int num = (Integer) session.getServletContext()
                .getAttribute("onLine");
        return "在线人数："+num;
    }

    @RequestMapping("destory")
    public String destroySession(HttpServletRequest request){
        HttpSession session = request.getSession();
        //强行销毁session
        session.invalidate();
        //从上下文中取出在线人数
        int num = (Integer) request.getServletContext()
                .getAttribute("onLine");
        return "在线人数："+num;
    }
}
